import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FetchService } from '../../../../../core/net/fetch.service';
import { FormBuilder, FormGroup } from '@angular/forms';
import { GateControlDetailComponent } from '../../gate-control-detail/gate-control-detail.component';
import { NzModalService } from 'ng-zorro-antd';

const stationHash = {
    '168': '跃进渠一所',
    '169': '跃进渠二所'
};

@Component({
    selector: 'app-gt-control-straight',
    templateUrl: './gt-control-straight.component.html',
    styleUrls: ['../gate-control-list.component.scss', './gt-control-straight.component.scss']
})
export class GtControlStraightComponent implements OnInit {
    tableData = [];
    pageNum = 1;
    pageSize = 10;
    total: number;
    loading = false;
    searchForm: FormGroup;
    sectionList = [];
    gateList = [];
    constructor(private router: Router,
                private fb: FormBuilder,
                private modalService: NzModalService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            watergateCode: [],
            sectionId: ['1'],
            section2Id: []
        });
    }

    ngOnInit(): void {
        //  查询管理段
        // this.fetchService.post<any>('sys/sysDictValue/list', { dictTypeId: '1022' })
        //     .subscribe(res => {
        //
        //     });
        this.getSectionList();
        // this.queryInfo();
    }

    getSectionList(): void {
        const params = {
            pageSize: 999
        };
        // 默认宁西公司的时候传参type=3
        if (this.searchForm.get('sectionId').value === '1') {
            params['type'] = 3;
        } else {
            params['pid'] = this.searchForm.get('sectionId').value;
        }
        this.searchForm.patchValue({
            section2Id: null
        });
        this.fetchService.post('wrs/wrsBmanage/list', params).subscribe(res => {
            this.sectionList = res['list'] || [];
            this.queryInfo(true);
        });
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            ...this.searchForm.value,
            // strobeName: '过滤info',
            pageNum: this.pageNum,
            pageSize: this.pageSize
        };
        // 默认宁西公司的时候传参sectionId为空
        if (this.searchForm.get('sectionId').value === '1') {
            params['sectionId'] = null;
        }
        this.fetchService.post('base/wrsBastraightOpening/list', params).subscribe(res => {
            const list = res['list'] || [];
            // const data = list.filter(item => {
            //     return !!item.info;
            // });
            this.tableData = list.map(row => {
                return {
                    ...row,
                    ...row.info,
                    stationName: stationHash[row.sectionId]
                };
            });
            this.total = res['total'];
            this.loading = false;
            this.fetchService.post<any>('base/wrsBrwaterDam/queryDamInfoList', {
                ...params,
                pageSize: 10000
            }).subscribe(resp => {
                this.gateList = resp['list'].map(gate => {
                    return {
                        ...gate,
                        degreeRate: parseFloat(gate.degree) / parseFloat(gate.strobeOpenHeight) * 100
                    };
                });
            });

        });
    }

    directToSketchMap(): void {
        this.router.navigate(['/gate/gt-control']);
    }

    openGateDetailModal(strobeCode): void {
        const data = this.gateList.find(item => {
            return item.stadiaCode == strobeCode;
        });
        const modal = this.modalService.create({
            nzTitle: data.watergateCode,
            nzClassName: 'custom-modal',
            nzWidth: '90%',
            nzBodyStyle: {
                background: '#eee',
                margin: 0,
                padding: '5px'
            },
            nzContent: GateControlDetailComponent,
            nzFooter: null,
            nzComponentParams: {
                data: data
            }
        });
        modal.afterOpen.subscribe(() => console.log('[afterOpen] emitted!'));

        // Return a result when closed
        modal.afterClose.subscribe(result => console.log('[afterClose] The result is:', result));

        // delay until modal instance created
        // setTimeout(() => {
        //     const instance = modal.getContentComponent();
        //     instance.subtitle = 'sub title is changed';
        // }, 2000);
    }
}
